package thitracnghiem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Odotube
 */
public class CauHoi {

    String maMonThi;
    String maCauHoi;
    String cauHoi;
    ArrayList<CauTraLoi> mListCauTraLoi;
    String dapAn;
    static Connection conn;
    static Statement stm;

    public CauHoi(String maCauHoi, String maMonThi, String cauHoi, String mTuyChon, int dapAn) {
        this.maCauHoi = maCauHoi;
        this.maMonThi = maMonThi;
        this.cauHoi = cauHoi;
        String[] strings = mTuyChon.split("//");
        ArrayList<CauTraLoi> mList;
        mList = new ArrayList<CauTraLoi>();
        char temp = 'a';
        for (int i = 1; i < dapAn; i++) {
            temp++;
        }
        this.dapAn = String.valueOf(temp);
        temp = 'a';

        for (int i = 0; i < strings.length; i++) {
            mList.add(new CauTraLoi(this.maCauHoi, "" + temp, strings[i]));
            temp++;
        }
        this.mListCauTraLoi = mList;
    }

    public static StringBuilder getViewDSCauHoi(String monThi) throws SQLException {
        CauHoi.Open();
        CauTraLoi.Open();
        ArrayList<CauHoi> mList = CauHoi.getDSCauHoi(monThi);
        StringBuilder s = new StringBuilder();
        s.append("<table width='100%' border=2><th>Danh sach cau hoi mon ").append(monThi);
        s.append("<th>Xoa<th>Sua");
        for (int i = 0; i < mList.size(); i++) {
            s.append("<tr><td with='80%'valign='center'>").append("Câu hỏi: " + mList.get(i).getCauHoi()).append("</td>");
            s.append("<td width='10%' valign='center' align='center'><a href=/ThiTracNghiem/XuLy?yc=quanlycauhoi&xoacauhoi=" + mList.get(i).getMaCauHoi() + ">Xoa</a></td>");
            s.append("<td width='10%' valign='center' align='center'><a href=/ThiTracNghiem/XuLy?yc=quanlycauhoi&suacauhoi=" + mList.get(i).getMaCauHoi() + ">Sua</a></td></tr>");
            for (int j = 0; j < mList.get(i).getListCauTraLoi().size(); j++) {
                s.append("<tr><td width='80%' valign='center'>").append("Tuy chon: " + mList.get(i).getListCauTraLoi().get(j).getLuaChon()).append("</td></tr>");
            }
            s.append("<tr><td width='80%' valign='center'>").append("Dap an: " + mList.get(i).getDapAn()).append("</td></tr>");

        }
        CauHoi.Close();
        CauTraLoi.Close();
        return s;
    }

    public static String genMaCauHoi(String maMonThi) throws SQLException, NumberFormatException {
        String sql = "select * from CauHoi where mamonthi='" + maMonThi + "'";
        int value = 0;
        ResultSet rs = stm.executeQuery(sql);
        while (rs.next()) {
            int temp = Integer.parseInt(rs.getString(1).substring(maMonThi.length()));
            if (temp > value) {
                value = temp;
            }
        }
        return maMonThi + String.valueOf(++value);
    }

    public CauHoi(String maCauHoi, String maMonThi, String cauHoi, ArrayList<CauTraLoi> mTuyChon, String dapAn) {
        this.maCauHoi = maCauHoi;
        this.maMonThi = maMonThi;
        this.cauHoi = cauHoi;
        this.mListCauTraLoi = mTuyChon;
        this.dapAn = dapAn;
    }

    public static boolean isDeleable(String maCauHoi) throws SQLException {
        String sql = "select dsmacauhoi from KetQuaThi";
        ResultSet rs = stm.executeQuery(sql);
        String temp = null;
        while (rs.next()) {
            temp = rs.getString("dsmacauhoi");
            if (temp != null) {
                if (temp.contains(String.valueOf(maCauHoi))) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void Open() throws SQLException {
        conn = DriverManager.getConnection("jdbc:odbc:ttn");
        stm = conn.createStatement();
    }

    public static void Close() throws SQLException {
        stm.close();
        conn.close();
    }

    public static boolean isDifferent(CauHoi ch1, CauHoi ch2) {
        if (!ch1.getCauHoi().equals(ch2.getCauHoi())) {
            return true;
        }
        if (ch1.getDapAn() == null ? ch2.getDapAn() != null : !ch1.getDapAn().equals(ch2.getDapAn())) {
            return true;
        }
        if (ch1.getListCauTraLoi().size() != ch2.getListCauTraLoi().size()) {
            return true;
        } else {
            for (int i = 0; i < ch1.getListCauTraLoi().size(); i++) {
                if (!ch1.getListCauTraLoi().get(i).getMaLuaChon().equals(ch1.getListCauTraLoi().get(i).getMaLuaChon())) {
                    return true;
                }
            }
        }
        return false;
    }

    public int insCauHoi() throws SQLException {
        System.out.println(cauHoi);
        String sql = "insert into CauHoi values('" + maCauHoi + "','" + maMonThi + "', N'" + cauHoi + "','" + dapAn + "')";
        Open();
        int temp = stm.executeUpdate(sql);

        if (temp <= 0) {
            return 0;
        } else {
            for (int i = 0; i < mListCauTraLoi.size(); i++) {
                mListCauTraLoi.get(i).insCauTraLoi();
            }
        }
        Close();
        return temp;
    }

    public int updateCauHoi() throws SQLException {
        CauTraLoi.delCauTraLoi(maCauHoi);
        for (int i = 0; i < mListCauTraLoi.size(); i++) {
            mListCauTraLoi.get(i).insCauTraLoi();
        }
        String sql = "update CauHoi set cauhoi='" + cauHoi + "',dapan='" + dapAn + "'";
        return stm.executeUpdate(sql);
        //  return 1;
    }

    public static int delCauHoi(String maCauHoi) throws SQLException {
        String sql = "delete CauHoi where macauhoi='" + maCauHoi + "'";
        CauTraLoi.delCauTraLoi(maCauHoi);
        return stm.executeUpdate(sql);

    }

    public static CauHoi getCauHoi(String maCauHoi) throws SQLException {
        String sql = "select * from CauHoi where macauhoi='" + maCauHoi + "'";
        Open();
        ResultSet rs = stm.executeQuery(sql);

        String temp = "";
        if (rs.next()) {
            temp = rs.getString(1);
            return new CauHoi(temp, rs.getString(2), rs.getString(3), CauTraLoi.getDSCauTraLoi(temp), rs.getString(4));
        }
        Close();
        return null;
    }

    public boolean isValid() {
        if (this.getDapAnInt() > mListCauTraLoi.size()) {
            return false;
        }
        return true;
    }

    public static ArrayList<CauHoi> getDSCauHoi(String maMonThi) throws SQLException {
        ArrayList<CauHoi> mList = new ArrayList<CauHoi>();
        String sql = "select * from CauHoi where mamonthi='" + maMonThi + "'";
        ResultSet rs = stm.executeQuery(sql);
        String temp = "";
        while (rs.next()) {
            temp = rs.getString(1);
            mList.add(new CauHoi(temp, rs.getString(2), rs.getString(3), CauTraLoi.getDSCauTraLoi(temp), rs.getString(4)));
        }
        return mList;
    }

    public void setMaCauHoi(String maCauHoi) {
        this.maCauHoi = maCauHoi;
    }

    public String getMaCauHoi() {
        return this.maCauHoi;
    }

    public void setMaMonThi(String maMonThi) {
        this.maMonThi = maMonThi;
    }

    public String getMaMonThi() {
        return this.maMonThi;
    }

    public void setCauHoi(String cauHoi) {
        this.cauHoi = cauHoi;
    }

    public String getCauHoi() {
        return this.cauHoi;
    }

    public void setListCauTraLoi(ArrayList<CauTraLoi> mTuyChon) {
        this.mListCauTraLoi = mTuyChon;
    }

    public ArrayList<CauTraLoi> getListCauTraLoi() {
        return this.mListCauTraLoi;
    }

    public void setDapAn(String dapAn) {
        this.dapAn = dapAn;
    }

    public String getDapAn() {
        return this.dapAn;
    }

    public int getDapAnInt() {
        char temp = 'a';
        int value = 1;
        for (int i = 0; i < 25; i++) {
            if (String.valueOf(temp).equals(this.dapAn)) {
                return value;
            }
            temp++;
            value++;

        }
        return -1;
    }

    public static void main(String[] args) throws SQLException {
        CauHoi ch = new CauHoi("cplus4", "cplus", "Al lô â ê", "á//à", 1);
        //ch.insCauHoi();
        System.out.println(CauHoi.getCauHoi("cplus4").getCauHoi());

    }
}
